import org.jets3t.service.impl.rest.httpclient.RestS3Service
import org.jets3t.service.security.AWSCredentials
import org.jets3t.service.model.*
import org.jets3t.service.acl.AccessControlList

bucketName =  'casework-files'
accessKey =  'not-real-access-key'
secretKey =  'donkey-done'

@Grab(group='net.java.dev.jets3t',module='jets3t',version='[0.8.1a,)')
public putS3() {}

def login = new AWSCredentials( accessKey, secretKey )
def s3Service = new RestS3Service( login )

 // List all your buckets.
def  buckets = s3Service.listAllBuckets();

buckets.each{ b ->
    def objects = s3Service.listObjects(b)

    objects.any{
        print "converting ${it.name}...."
        if(it.name.contains('pdf')){
            targetObject = new S3Object(it.name);
            targetObject.addMetadata(S3Object.METADATA_HEADER_CONTENT_TYPE, "application/pdf");
            targetObject.setAcl AccessControlList.REST_CANNED_PUBLIC_READ
            replaceMetadata = true;
            s3Service.copyObject(bucketName, it.name, bucketName, targetObject, replaceMetadata);
            println "done"
        } else{
            println "not needed"
        }

    }
}